package com.ruoyi.web.controller.basedata;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.basedata.domain.UsersDb;
import com.ruoyi.basedata.service.IUsersDbService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 大用户档案Controller
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/basedata/usersdb")
public class UsersDbController extends BaseController
{
    @Autowired
    private IUsersDbService usersDbService;

    /**
     * 查询大用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:list')")
    @GetMapping("/list")
    public TableDataInfo list(UsersDb usersDb)
    {
        startPage();
        List<UsersDb> list = usersDbService.selectUsersDbList(usersDb);
        return getDataTable(list);
    }

    /**
     * 用户下拉框
     * @param usersDb
     * @return
     */
    @GetMapping("/listDropDown")
    public AjaxResult listDropDown()
    {
        List<UsersDb> list = usersDbService.list(new QueryWrapper<UsersDb>().eq("DeleteMark",0).eq("UserMark",1));
        return AjaxResult.success(list);
    }



    /**
     * 导出大用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:export')")
    @Log(title = "大用户档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UsersDb usersDb)
    {
        List<UsersDb> list = usersDbService.selectUsersDbList(usersDb);
        ExcelUtil<UsersDb> util = new ExcelUtil<UsersDb>(UsersDb.class);
        util.exportExcel(response, list, "大用户档案数据");
    }

    /**
     * 获取大用户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(usersDbService.selectUsersDbByID(ID));
    }

    /**
     * 新增大用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:add')")
    @Log(title = "大用户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UsersDb usersDb)
    {
        List<UsersDb> list = usersDbService.list(new QueryWrapper<UsersDb>().eq("UserID",usersDb.getUserID()).eq("DeleteMark",0).eq("UserMark",1));
        if(null!=list && list.size()>0){
            return AjaxResult.error("用户编号重复！");
        }
        return toAjax(usersDbService.insertUsersDb(usersDb));
    }

    /**
     * 修改大用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:edit')")
    @Log(title = "大用户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UsersDb usersDb)
    {
        return toAjax(usersDbService.updateUsersDb(usersDb));
    }

    /**
     * 删除大用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:remove')")
    @Log(title = "大用户档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(usersDbService.deleteUsersDbByIDs(IDs));
    }


    /**
     * 修改大用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:usersdb:edit')")
    @Log(title = "大用户档案", businessType = BusinessType.UPDATE)
    @PutMapping("/closingAccount")
    public AjaxResult closingAccount(Long ID)
    {
        return toAjax( usersDbService.closingAccount(ID));
    }


    @Log(title = "导入大表用户", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<UsersDb> util = new ExcelUtil<UsersDb>(UsersDb.class);
        List<UsersDb> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = usersDbService.importUser(userList, updateSupport, operName);
        return success(message);
    }
}
